package com.pzr.soccer.channel.rabbit.receiver;

import com.alibaba.fastjson.JSON;
import com.pzr.soccer.channel.tio.WebSocketMessageHandler;
import com.pzr.soccer.match.model.GameWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tio.core.Tio;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.starter.TioWebSocketServerBootstrap;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author pzr
 * @date:2021-12-18-11:24
 * @Description:
 **/
@Component
@Slf4j
public class CrawlerInfoReceiver {
    @Autowired
    TioWebSocketServerBootstrap tioWebSocketServerBootstrap;

    @RabbitListener(queues = "Queue_Today_Game_Changed_Slim")
    public void processChangeGames(GameWrapper wrapper){
        log.info("接收到 Crawler game slim changed   : " + JSON.toJSONString(wrapper));
        Map<String,Object> pack=new HashMap<>();
        pack.put("tag", WebSocketMessageHandler.Channel_Tag_ChangedGame);
        pack.put("data",wrapper);
        WsResponse wsResponse = WsResponse.fromText(JSON.toJSONString(pack), "utf-8");
        Tio.sendToGroup(tioWebSocketServerBootstrap.getServerTioConfig(),WebSocketMessageHandler.Channel_Group_Public, wsResponse);
    }

    @RabbitListener(queues = "Queue_Today_Game_State_Changed")
    public void processChangedStates(Map<String,Integer> map){
        log.info("接收到 Crawler state changed  : " + JSON.toJSONString(map));
        Map<String,Object> pack=new HashMap<>();
        pack.put("tag", WebSocketMessageHandler.Channel_Tag_ChangedState);
        pack.put("data",map);
        WsResponse wsResponse = WsResponse.fromText(JSON.toJSONString(pack), "utf-8");
        Tio.sendToGroup(tioWebSocketServerBootstrap.getServerTioConfig(),WebSocketMessageHandler.Channel_Group_Public, wsResponse);
    }
}
